//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - S+S Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeUI;
using LitwareHR.Infrastructure.Interface;
using Microsoft.Practices.CompositeUI.EventBroker;
using LitwareHR.PublicApp.WinUX.Constants;

using System.Reflection;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using LitwareHR.Infrastructure.Interface.Constants;

namespace LitwareHR.PublicApp.WinUX
{
    public partial class LinkViewPresenter : Presenter<ILinkView>
    {
        [EventPublication(EventTopicNames.ShowOpenPositionView, PublicationScope.Global)]
        public event EventHandler<EventArgs> ShowOpenPositionView;

        [EventPublication(EventTopicNames.Authenticate, PublicationScope.Global)]
        public event EventHandler<EventArgs> Authenticate;

        [EventPublication(EventTopicNames.ShowJobSearchView, PublicationScope.Global)]
        public event EventHandler<EventArgs> ShowJobSearchView;

        [EventPublication(EventTopicNames.ShowDefaultView, PublicationScope.Global)]
        public event EventHandler<EventArgs> ShowDefaultView;

        [EventPublication(EventTopicNames.ShowCandidatesView, PublicationScope.Global)]
        public event EventHandler<EventArgs> ShowCandidatesView;

        /// <summary>
        /// This method is a placeholder that will be called by the view when it has been loaded.
        /// </summary>
        public override void OnViewReady()
        {
            base.OnViewReady();

            BindData();
        }

        private void BindData()
        {
            CommandMap map = LoadCommandMap();
            View.BindData(map);
        }
        /// <summary>
        /// This method reads CommandMap.xml and populates CommandMap object
        /// </summary>
        /// <returns></returns>
        private static CommandMap LoadCommandMap()
        {
            string resourceName = "LitwareHR.PublicApp.WinUX.Views.LinkView.CommandMap.xml";
            Assembly assembly = Assembly.GetExecutingAssembly();
            Stream xmlStream = assembly.GetManifestResourceStream(resourceName);
            XmlSerializer serializer = new XmlSerializer(typeof(CommandMap));
            CommandMap map = (CommandMap)serializer.Deserialize(xmlStream);
            return map;
        }

        /// <summary>
        /// Close the view
        /// </summary>
        public void OnCloseView()
        {
            base.CloseView();
            WorkItem.SmartParts.Remove(View);
            if (View is IDisposable)
                ((IDisposable)View).Dispose();

        }

        protected virtual void OnAuthenticate(EventArgs eventArgs)
        {
            if (Authenticate != null)
            {
                Authenticate(this, eventArgs);
            }
        }

        protected virtual void OnShowJobSearchView(EventArgs eventArgs)
        {
            if (ShowJobSearchView != null)
            {
                ShowJobSearchView(this, eventArgs);
            }
        }

        protected virtual void OnShowOpenPositionView(EventArgs eventArgs)
        {
            if (ShowOpenPositionView != null)
            {
                ShowOpenPositionView(this, eventArgs);
            }
        }

        protected virtual void OnShowDefaultView(EventArgs eventArgs)
        {
            if (ShowDefaultView != null)
            {
                ShowDefaultView(this, eventArgs);
            }
        }

        protected virtual void OnShowCandidatesView(EventArgs eventArgs)
        {
            if (ShowCandidatesView != null)
            {
                ShowCandidatesView(this, eventArgs);
            }
        }

        /// <summary>
        /// This method displays View corresponding to the request (e.Data)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void RaiseShowView(object sender, EventArgs<String> e)
        {
            switch (e.Data)
            {
                case "OnJobSearch":
                    OnShowJobSearchView(null);
                    break;
                case "OnOpenPosition":
                    OnShowOpenPositionView(null);
                    break;
                case "OnHome":
                    OnShowDefaultView(null);
                    break;
                case "OnCandidates":
                    OnShowCandidatesView(null);
                    break;
            }
        }
    }
}

